/**
 * @author : 乔磊磊
 * @ide    : WebStorm
 * @date   : 2025/2/23 星期日 18:43
 * @desc   :
 */
import {find, instantiate, Node} from "cc"
import {loadNode, loadPrefab} from "../asset";
import {logger} from "../log";
import {IUIViewParam} from "./IUIViewParam"
import {UIViewBase} from "./UIViewBase"

export class PopViewMgr {
    private _popQueue: Array<any> = [] //弹窗队列
    // 弹出窗口
    private _popping = false

    // 单例模式
    private static _instance: PopViewMgr

    public static get instance(): PopViewMgr {
        if (this._instance == null) {
            this._instance = new PopViewMgr()
        }
        return this._instance
    }

    public PopView(uiViewParam: IUIViewParam, arg?: any, parent?: Node) {
        if (this._popping) return
        if (uiViewParam.queue) {
            if (this.checkInQueue(uiViewParam)) return
            this._popQueue.push({prefabDetails: uiViewParam, arg: arg, parent: parent})
            if (this._popQueue.length > 1) return
            this._popping = true
            this.popNextView({
                prefabDetails: uiViewParam,
                arg: arg,
                parent: parent,
            }).then(() => this._popping = false)
        } else {
            this._popping = true
            this.popNextView({prefabDetails: uiViewParam, arg: arg, parent: parent}).then(() => this._popping = false)
            return
        }
    }

    /**
     * 飘一个提示文本
     * @param str 要显示的文本
     */
    public showTips(str: string) {
        /* let parent = this.getParentNode()
         ResourceMgr.loadPrefab("TipAndProgress/ToastTip").then((prefab) => {
             let tip = instantiate(prefab)
             tip.setParent(parent)
             let tipScript = tip.getComponent(ToastTip)
             tipScript.showTip(str, 2)
         })*/
    }

    /*弹出窗口*/
    private async popNextView(data: any) {
        let prefabDetails = data.prefabDetails as IUIViewParam
        let parent = data.parent
        if (parent == null) parent = this.getParentNode()
        logger.log("[popView]: " + prefabDetails.prefabPath)
        // 加载预制体
        let node = await loadNode(prefabDetails.prefabPath)
        let script = node.getComponent(UIViewBase)
        script?.setViewInfo(prefabDetails, () => {
            this.closeView(prefabDetails)
        })
        parent.addChild(node)
    }

    /*关闭窗口*/
    private closeView(prefabDetails: IUIViewParam) {
        if (prefabDetails.queue && this._popQueue.length > 0) {
            this._popQueue.shift()
            if (this._popQueue.length > 0) {
                this.popNextView(this._popQueue[0]).then(() => undefined)
            }
        }
    }

    private checkInQueue(prefabDetails: IUIViewParam) {
        for (let i = 0; i < this._popQueue.length; i++) {
            if (this._popQueue[i].prefabDetails.prefabPath == prefabDetails.prefabPath) return true
        }
        return false
    }

    private getParentNode(): Node | null {
        return find("Canvas/PopLayerBase") || find("Canvas")
    }
}
